﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using comExcel = Microsoft.Office.Interop.Excel;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Windows.Forms;
using System.IO;

namespace QLTV_V2
{
    class ExportToExcel
    {   
        //
        public void exportExcel_2(DataGridView dgv, DataTable dtb, int dk)
        {
            // TẠO MỚI FILE EXCEL
            comExcel._Application app = new Microsoft.Office.Interop.Excel.Application();
            //Tạo mới workbook
            comExcel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            object misValue = System.Reflection.Missing.Value;
            // Tạo mới 1 sheet
            comExcel.Worksheet worksheet = null;
           // app.Visible = true;

            // get the reference of first sheet. By default its name is file mau
            worksheet = (comExcel.Worksheet)workbook.Worksheets.get_Item(1);
            // changing the name of active sheet
            worksheet.Name = "File mau Ban doc";
            if (dk == 1) //LAY DỮ LIỆU TỪ DATAGRIDVIEW
            {
                // Tạo header
                for (int i = 1; i < dgv.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = dgv.Columns[i - 1].HeaderText;
                }

                for (int i = 0; i < dgv.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgv.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dgv.Rows[i].Cells[j].Value.ToString();
                    }
                }
                for (int i = 0; i < dgv.Rows.Count - 1; i++)
                    ((comExcel.Range)worksheet.Cells[1, i + 1]).EntireColumn.AutoFit();
            }
            else //Lấu DL từ datatable
            {
                int cot = dtb.Columns.Count;
                int hang = dtb.Rows.Count;
                //Tạo header
                for (int i = 1; i <= dtb.Columns.Count; i++)
                {
                    worksheet.Cells[1, i] = dtb.Columns[i-1].ColumnName;
                }
                //
                for (int i = 0; i < hang; i++)
                {
                    for (int j = 0; j < cot; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = dtb.Rows[i][j];
                    }
                }
                for (int i = 0; i < hang; i++)
                    ((comExcel.Range)worksheet.Cells[1, i + 1]).EntireColumn.AutoFit();

            }
            SaveFileDialog f = new SaveFileDialog();
            f.Filter = "Excel file (*.xls)|*.xls";
            if (f.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    workbook.SaveAs(f.FileName, comExcel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, comExcel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    workbook.Close(true, misValue, misValue);
                    MessageBox.Show("Thành công", "Thông báo");
                }
                catch {
                    MessageBox.Show("Đặt tên khác","Thông báo");
                }
               // workbook.Quit();
            }
        }
    }
}
